<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"/>




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  




<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Mvc5入门学习," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="怎样添加控制器与创建视图文件  首先我们打开接着上一篇的BlogManage解决方案，然后选中解决方案资源管理器中Controllers的文件夹，点击鼠标右键，添加-&amp;gt;控制器，如下：  
  在弹出的选择框中：  
  我们选择MVC5控制器-空，点击添加：  
  这里我们将控制器名称命名为图上所示，注意控制器名称是以Controller关键词结尾的，但访问控制器的时候是不需要输入Cont">
<meta property="og:type" content="article">
<meta property="og:title" content="Mvc5入门学习之怎样添加控制器与创建视图文件">
<meta property="og:url" content="http://ycdo.github.com/2016/09/24/Mvc5入门学习之怎样添加控制器与创建视图文件/index.html">
<meta property="og:site_name" content="勤能补拙">
<meta property="og:description" content="怎样添加控制器与创建视图文件  首先我们打开接着上一篇的BlogManage解决方案，然后选中解决方案资源管理器中Controllers的文件夹，点击鼠标右键，添加-&amp;gt;控制器，如下：  
  在弹出的选择框中：  
  我们选择MVC5控制器-空，点击添加：  
  这里我们将控制器名称命名为图上所示，注意控制器名称是以Controller关键词结尾的，但访问控制器的时候是不需要输入Cont">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addcontroller.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addcontroller1.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addcontroller2.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addcontroller3.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addcontroller4.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addcontroller7.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addcontroller8.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addstartview.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addstartview1.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addlayoutview.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addlayoutview1.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addupdate4.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addview3.png">
<meta property="og:image" content="http://ycdo.github.com/img/2016/9/addview2.png">
<meta property="og:updated_time" content="2016-09-26T02:29:25.248Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mvc5入门学习之怎样添加控制器与创建视图文件">
<meta name="twitter:description" content="怎样添加控制器与创建视图文件  首先我们打开接着上一篇的BlogManage解决方案，然后选中解决方案资源管理器中Controllers的文件夹，点击鼠标右键，添加-&amp;gt;控制器，如下：  
  在弹出的选择框中：  
  我们选择MVC5控制器-空，点击添加：  
  这里我们将控制器名称命名为图上所示，注意控制器名称是以Controller关键词结尾的，但访问控制器的时候是不需要输入Cont">
<meta name="twitter:image" content="http://ycdo.github.com/img/2016/9/addcontroller.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 6295473888616777000,
      author: 'YCDO'
    }
  };
</script>

  <title> Mvc5入门学习之怎样添加控制器与创建视图文件 | 勤能补拙 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">勤能补拙</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">工欲善其事，必先利其器</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-home fa-fw"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-archive fa-fw"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-tags fa-fw"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-th fa-fw"></i> <br />
            
            分类
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Mvc5入门学习之怎样添加控制器与创建视图文件
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-24T17:23:07+08:00" content="2016-09-24">
              2016-09-24
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Asp-Net/" itemprop="url" rel="index">
                    <span itemprop="name">Asp.Net</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/09/24/Mvc5入门学习之怎样添加控制器与创建视图文件/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/09/24/Mvc5入门学习之怎样添加控制器与创建视图文件/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="怎样添加控制器与创建视图文件"><a href="#怎样添加控制器与创建视图文件" class="headerlink" title="怎样添加控制器与创建视图文件"></a>怎样添加控制器与创建视图文件</h3><p>  首先我们打开接着上一篇的BlogManage解决方案，然后选中解决方案资源管理器中Controllers的文件夹，点击鼠标右键，添加-&gt;控制器，如下：<br>  <img src="/img/2016/9/addcontroller.png" alt="添加控制器 Icon"></p>
<p>  在弹出的选择框中：<br>  <img src="/img/2016/9/addcontroller1.png" alt="添加控制器 Icon"></p>
<p>  我们选择MVC5控制器-空，点击添加：<br>  <img src="/img/2016/9/addcontroller2.png" alt="添加控制器 Icon"></p>
<p>  这里我们将控制器名称命名为图上所示，注意控制器名称是以Controller关键词结尾的，但访问控制器的时候是不需要输入Controller后缀名的，就像这样：<strong> /Blog/Index </strong>（<em> MVC应用程序访问页面的格式是：/控制器名称/方法名称，当然也可以通过修改路由配置修改访问格式，这个放在下一篇来讲 </em>），说到这里有的人又会想为什么要加Controller，不加后缀又会如何，能这样想说明你很有潜质，就是要像这样，遇到问题我们要究其根本，这样我们就会一步一个脚印的把知识积累起来。</p>
<p>  那回过头来，为什么要加后缀，这是因为当我们访问/Blog/Index的时候，应用程序会搜索所有后缀名为Controller的公共类，判断是否存在Blog控制器，如果存在仅有的一个Blog控制器，就在这个控制器中查找是否存在Index的公共方法（<em> 一般方法的返回值类型为ActionResult </em>），如果存在多个Blog控制器，应用程序就会抛出控制器在多个命名空间下的错误。那如果不加后缀应用程序就会把它当成一个普通的类处理。</p>
<p>  然后我们点击添加：<br>  <img src="/img/2016/9/addcontroller3.png" alt="添加控制器 Icon"></p>
<p>  之后我们会发现它会在Controllers中添加一个BlogController的控制器，然后再Views文件夹下面它会自动添加一个名称为Blog的文件夹，它的作用是Blog控制器中的视图文件都会在这里添加，当然如果有必要的话，也可以不放在这里，放在这里是让应用程序层次更加清晰。</p>
<p>  接下来我们在Models中手动添加一个公共的类，结构如下：<br>  <img src="/img/2016/9/addcontroller4.png" alt="添加模型 Icon"></p>
<p>  代码如下：<br>  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">using System;</span><br><span class="line">using System.Collections.Generic;</span><br><span class="line">using System.Linq;</span><br><span class="line">using System.Web;</span><br><span class="line"></span><br><span class="line">namespace BlogManage.Models</span><br><span class="line">&#123;</span><br><span class="line">	public class BlogModel</span><br><span class="line">	&#123;</span><br><span class="line">		private string title;</span><br><span class="line">		/// &lt;summary&gt;</span><br><span class="line">		/// 博客标题</span><br><span class="line">		/// &lt;/summary&gt;</span><br><span class="line">		public string Title</span><br><span class="line">		&#123;</span><br><span class="line">			get &#123; return title; &#125;</span><br><span class="line">			set &#123; title = value; &#125;</span><br><span class="line">		&#125;</span><br><span class="line">		private string author;</span><br><span class="line">		/// &lt;summary&gt;</span><br><span class="line">		/// 博客作者</span><br><span class="line">		/// &lt;/summary&gt;</span><br><span class="line">		public string Author</span><br><span class="line">		&#123;</span><br><span class="line">			get &#123; return author; &#125;</span><br><span class="line">			set &#123; author = value; &#125;</span><br><span class="line">		&#125;</span><br><span class="line">		private string category;</span><br><span class="line">		/// &lt;summary&gt;</span><br><span class="line">		/// 博客分类</span><br><span class="line">		/// &lt;/summary&gt;</span><br><span class="line">		public string Category</span><br><span class="line">		&#123;</span><br><span class="line">			get &#123; return category; &#125;</span><br><span class="line">			set &#123; category = value; &#125;</span><br><span class="line">		&#125;</span><br><span class="line">		private DateTime creationtime;</span><br><span class="line">		/// &lt;summary&gt;</span><br><span class="line">		/// 发表时间</span><br><span class="line">		/// &lt;/summary&gt;</span><br><span class="line">		public DateTime CreationTime</span><br><span class="line">		&#123;</span><br><span class="line">			get &#123; return creationtime; &#125;</span><br><span class="line">			set &#123; creationtime = value; &#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>  然后我们在Blog控制器中方法Index中手动添加model对象 :<br>  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">using System;</span><br><span class="line">using System.Collections.Generic;</span><br><span class="line">using System.Linq;</span><br><span class="line">using System.Web;</span><br><span class="line">using System.Web.Mvc;</span><br><span class="line"></span><br><span class="line">using BlogManage.Models;</span><br><span class="line"></span><br><span class="line">namespace BlogManage.Controllers</span><br><span class="line">&#123;</span><br><span class="line">	public class BlogController : Controller</span><br><span class="line">	&#123;</span><br><span class="line">		//</span><br><span class="line">		// GET: /Blog/</span><br><span class="line">		public ActionResult Index()</span><br><span class="line">		&#123;</span><br><span class="line">			List&lt;BlogModel&gt; model = new List&lt;BlogModel&gt;()</span><br><span class="line">			&#123;</span><br><span class="line">				 new BlogModel()&#123;</span><br><span class="line">					Title=&quot;MVC模式&quot;,</span><br><span class="line">					Author=&quot;YCDO&quot;,</span><br><span class="line">					Category=&quot;Asp.Net&quot;,</span><br><span class="line">					CreationTime=new DateTime(2016,9,21)</span><br><span class="line">				&#125;,</span><br><span class="line">				 new BlogModel()&#123;</span><br><span class="line">					Title=&quot;项目工程结构介绍&quot;,</span><br><span class="line">					Author=&quot;YCDO&quot;,</span><br><span class="line">					Category=&quot;Asp.Net&quot;,</span><br><span class="line">					CreationTime=new DateTime(2016,9,22)</span><br><span class="line">				&#125;,</span><br><span class="line">				new BlogModel()&#123;</span><br><span class="line">					Title=&quot;怎样添加控制器与创建视图&quot;,</span><br><span class="line">					Author=&quot;YCDO&quot;,</span><br><span class="line">					Category=&quot;Asp.Net&quot;,</span><br><span class="line">					CreationTime=new DateTime(2016,9,23)</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;;</span><br><span class="line">			return View(model);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>  其中return View(model)这样默认的写法会在MVC生成的Blog文件夹中寻找与方法同名即Index.cshtml视图，我们也可以像这样return View(“~/Views/Blog/Index.cshtml”,model)传递视图路径参数来选择响应视图 。<br>注意这里我们需要引用<strong> BlogManager.Models </strong>的命名空间。</p>
<p>然后再Index方法区域鼠标右键，选择添加视图：<br><img src="/img/2016/9/addcontroller7.png" alt="添加视图 Icon"><br>取消勾选使用布局页选项，点击添加按钮，然后会在Views/Blog文件夹中生成一个名称为Index.cshtml的文件：<br><img src="/img/2016/9/addcontroller8.png" alt="添加视图 Icon"><br>如果Layout报红线，可以鼠标选中解决方案，右键生成解决方案，Index.cshtml关闭后重新打开即可。</p>
<p>由于我们创建应用时选择的是MVC5空项目，所以项目中没有布局页_Layout.cshtml（<em> 母版页 </em>）与_ViewStart.cshtml（<em> 进行视图渲染时先执行此页面，可以把页面的布局路径在这里设置 </em>）文件，这样我们手动添加一下:<br><img src="/img/2016/9/addstartview.png" alt="添加布局页 Icon"><br>命名为_ViewStart：<br><img src="/img/2016/9/addstartview1.png" alt="添加布局页 Icon"><br>这个视图文件里可以在视图启动开始时先执行，所以我们一般把视图布局路径写在这里：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">@&#123;</span><br><span class="line">    Layout = &quot;~/Views/Shared/_Layout.cshtml&quot;; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>然后选中Views，鼠标右键创建布局文件夹Shared，然后创建_Layout.cshtml布局文件：<br><img src="/img/2016/9/addlayoutview.png" alt="添加布局页 Icon"></p>
<p>命名为_Layout：<br><img src="/img/2016/9/addlayoutview1.png" alt="添加布局页 Icon"><br>点击确定：<br><img src="/img/2016/9/addupdate4.png" alt="添加布局页 Icon"><br>  然后我们有可能会看见上图所示，ViewBag.Title（<em> 页面标题，在内容页面定义赋值 </em>）与RenderBody()（<em> 内容页面代码的占位符 </em>）会出现不存在的错误，事实上是对运行时没有影响的，但是编写的时候就没有感知提示了（<em> 如果没有错误就跳过这一步 </em>），那怎样做才能修复这样的错误，一般有三种情况：<br><strong> 情况一：</strong>需要确认Views下面的Web.Config中MVC的版本信息与我们使用的版本号一致。例如我的是这样：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot;?&gt;</span><br><span class="line"></span><br><span class="line">&lt;configuration&gt;</span><br><span class="line">  &lt;configSections&gt;</span><br><span class="line">    &lt;sectionGroup name=&quot;system.web.webPages.razor&quot; type=&quot;System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;&gt;</span><br><span class="line">      &lt;section name=&quot;host&quot; type=&quot;System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirePermission=&quot;false&quot; /&gt;</span><br><span class="line">      &lt;section name=&quot;pages&quot; type=&quot;System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirePermission=&quot;false&quot; /&gt;</span><br><span class="line">    &lt;/sectionGroup&gt;</span><br><span class="line">  &lt;/configSections&gt;</span><br><span class="line"></span><br><span class="line">  &lt;system.web.webPages.razor&gt;</span><br><span class="line">    &lt;host factoryType=&quot;System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; /&gt;</span><br><span class="line">    &lt;pages pageBaseType=&quot;System.Web.Mvc.WebViewPage&quot;&gt;</span><br><span class="line">      &lt;namespaces&gt;</span><br><span class="line">        &lt;add namespace=&quot;System.Web.Helpers&quot; /&gt;</span><br><span class="line">        &lt;add namespace=&quot;System.Web.Mvc&quot; /&gt;</span><br><span class="line">        &lt;add namespace=&quot;System.Web.Mvc.Ajax&quot; /&gt;</span><br><span class="line">        &lt;add namespace=&quot;System.Web.Mvc.Html&quot; /&gt; </span><br><span class="line">        &lt;add namespace=&quot;System.Web.Routing&quot; /&gt;</span><br><span class="line">        &lt;add namespace=&quot;System.Web.WebPages&quot; /&gt;</span><br><span class="line">        &lt;add namespace=&quot;System.Web.WebPages.Razor&quot; /&gt;</span><br><span class="line">        &lt;add namespace=&quot;BlogManage.Models&quot; /&gt;</span><br><span class="line">      &lt;/namespaces&gt;</span><br><span class="line">    &lt;/pages&gt;</span><br><span class="line">  &lt;/system.web.webPages.razor&gt;</span><br><span class="line"></span><br><span class="line">  &lt;appSettings&gt;</span><br><span class="line">    &lt;add key=&quot;webpages:Enabled&quot; value=&quot;false&quot; /&gt;</span><br><span class="line">  &lt;/appSettings&gt;</span><br><span class="line"></span><br><span class="line">  &lt;system.webServer&gt;</span><br><span class="line">    &lt;handlers&gt;</span><br><span class="line">      &lt;remove name=&quot;BlockViewHandler&quot;/&gt;</span><br><span class="line">      &lt;add name=&quot;BlockViewHandler&quot; path=&quot;*&quot; verb=&quot;*&quot; preCondition=&quot;integratedMode&quot; type=&quot;System.Web.HttpNotFoundHandler&quot; /&gt;</span><br><span class="line">    &lt;/handlers&gt;</span><br><span class="line">  &lt;/system.webServer&gt;</span><br><span class="line">&lt;/configuration&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong> 情况二：</strong>我们需要使用菜单项工具-&gt;NuGet程序包管理器-&gt;程序包管理器控制台更新一下MVC版本（<em> 我这里执行了Install-Package Microsoft.AspNet.Mvc –Version 5.2.3，之后F5运行程序，_Layout.cshtml须关闭后重新打开，问题解决 </em>）。<br><strong> 情况三：</strong>需要关闭Visual Studio，然后我的电脑-&gt;地址栏输入 %UserProfile%\AppData\Local\Microsoft\VisualStudio\，在这些文件夹中找到文件夹名称中含有 ComponentModelCache 的文件夹 删除或者重新命名。</p>
<p>在布局页_Layout.cshtml面我们稍微加一点样式，像这样：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line"></span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot; /&gt;</span><br><span class="line">    &lt;title&gt;@ViewBag.Title&lt;/title&gt;</span><br><span class="line">    &lt;link href=&quot;//cdn.bootcss.com/bootstrap/4.0.0-alpha.3/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;header&quot; style=&quot;margin-top: 300px&quot;&gt;</span><br><span class="line">            &lt;h3 class=&quot;text-muted&quot;&gt;怎样添加控制器与创建视图文件&lt;/h3&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">        &lt;div class=&quot;jumbotron&quot;&gt;</span><br><span class="line">            @RenderBody()</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p>我的是访问网络的<a href="http://www.bootcdn.cn/" target="_blank" rel="external">Bootstrap的免费CDN</a>（<em> 全称是Content Delivery Network，即内容分发网络 </em>）服务，当然你也可以把Bootsrap下载到项目中，还是菜单项工具-&gt;NuGet程序包管理器-&gt;程序包管理器控制台，输入Install-Package Bootstrap，它会自动安装Bootstrap样式表与脚本（<em> 前提是需要联网 </em>）。</p>
<p>然后我们再修改Blog/Index视图页面<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@&#123;</span><br><span class="line">    // Layout = null;　 </span><br><span class="line">&#125;</span><br><span class="line">@model  IEnumerable&lt;BlogModel&gt;</span><br><span class="line">@if (Model != null &amp;&amp; Model.Count() &gt; 0)</span><br><span class="line">&#123;</span><br><span class="line">    foreach (var item in Model)</span><br><span class="line">    &#123;</span><br><span class="line">    &lt;div&gt;@item.Author 在 @item.CreationTime 发布了一篇 @item.Title 的文章，分类与 @item.Category&lt;/div&gt;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>这里我们把布局页面路径写在_ViewStart.cshtml中，这里就注释掉。注意IEnumerable<blogmodel>的BlogModel可能会报不存在的错，这里需要在Views/web.config中添加引用BlogManage.Models的命名空间，然后重新生成项目即可。<br> <img src="/img/2016/9/addview3.png" alt="视图添加命名空间 Icon"></blogmodel></p>
<p>最后一步打开App_Start-&gt;RouteConfig.cs文件，我们把controller=”Home”的Home改为Blog（<em> 设置应用程序启动页面为：/Blog/Index </em>）:<br> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">using System;</span><br><span class="line">using System.Collections.Generic;</span><br><span class="line">using System.Linq;</span><br><span class="line">using System.Web;</span><br><span class="line">using System.Web.Mvc;</span><br><span class="line">using System.Web.Routing;</span><br><span class="line"></span><br><span class="line">namespace BlogManage</span><br><span class="line">&#123;</span><br><span class="line">    public class RouteConfig</span><br><span class="line">    &#123;</span><br><span class="line">        public static void RegisterRoutes(RouteCollection routes)</span><br><span class="line">        &#123;</span><br><span class="line">            routes.IgnoreRoute(&quot;&#123;resource&#125;.axd/&#123;*pathInfo&#125;&quot;);</span><br><span class="line"></span><br><span class="line">            routes.MapRoute(</span><br><span class="line">                name: &quot;Default&quot;,</span><br><span class="line">                url: &quot;&#123;controller&#125;/&#123;action&#125;/&#123;id&#125;&quot;,</span><br><span class="line">                defaults: new &#123; controller = &quot;Blog&quot;, action = &quot;Index&quot;, id = UrlParameter.Optional &#125;</span><br><span class="line">            );</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>  然后F5运行。<br>  <img src="/img/2016/9/addview2.png" alt="怎样添加控制器与创建视图文件 Icon"></p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>如果你按照上面的顺序做的话，基本上都可以完成这部分，可能NuGet程序包管理这里不好操作，但是这个一定要学会，这个对项目的引用配置非常有用，安装程序包的时候不需要额外去搜索下载，只需要敲一敲命令就可以操作，实在是方便。下一篇将介绍路由的配置。</p>

      
    </div>
    
    <div>
      
        
      
    </div>

    <div>
      
        
      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Mvc5入门学习/" rel="tag">#Mvc5入门学习</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/22/Mvc5入门学习之项目工程结构介绍/" rel="next" title="Mvc5入门学习之项目工程结构介绍">
                <i class="fa fa-chevron-left"></i> Mvc5入门学习之项目工程结构介绍
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/10/Mvc5入门学习之路由配置/" rel="prev" title="Mvc5入门学习之路由配置">
                Mvc5入门学习之路由配置 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/09/24/Mvc5入门学习之怎样添加控制器与创建视图文件/"
     data-title="Mvc5入门学习之怎样添加控制器与创建视图文件"
     data-content=""
     data-url="http://ycdo.github.com/2016/09/24/Mvc5入门学习之怎样添加控制器与创建视图文件/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          

  <p>热评文章</p>
  <div class="ds-top-threads" data-range="weekly" data-num-items="4"></div>


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/09/24/Mvc5入门学习之怎样添加控制器与创建视图文件/"
           data-title="Mvc5入门学习之怎样添加控制器与创建视图文件" data-url="http://ycdo.github.com/2016/09/24/Mvc5入门学习之怎样添加控制器与创建视图文件/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://zhangbaolei.com/images/avatar2.png"
               alt="张宝蕾" />
          <p class="site-author-name" itemprop="name">张宝蕾</p>
          <p class="site-description motion-element" itemprop="description">工欲善其事，必先利其器</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">8</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#怎样添加控制器与创建视图文件"><span class="nav-number">1.</span> <span class="nav-text">怎样添加控制器与创建视图文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#总结"><span class="nav-number">2.</span> <span class="nav-text">总结</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">张宝蕾</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"ycdo"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
      
      <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
      <script src="/js/src/hook-duoshuo.js"></script>
    
  





  
  
  

  

  

</body>
</html>
